package geon.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import geon.model.Endereco;

public class DaoEndereco implements Dao<Endereco> {
	
	private DaoGenerico dao;
	private String sql;
	
	public DaoEndereco() {
		dao = new DaoGenerico();
	}

	@Override
	public Integer add(Endereco o) throws SQLException {
		try {
			dao.begin();
			sql = "Insert Into TbEndereco (EndCodigo, LogCodigo, EndNumero, EndComplemento) Values (?, ?, ?, ?)";
			o.setCodigo(dao.getNextId("TbEndereco", "EndCodigo"));
			dao.executeCommand(sql, o.getCodigo(), o.getLogradouro().getCodigo(), o.getNumero(), o.getComplemento());
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
		return o.getCodigo();
	}

	@Override
	public void upd(Endereco o) throws SQLException {
		try {
			dao.begin();
			sql = "Update TbEndereco Set LogCodigo = ?, EndNumero = ?, EndComplemento = ? Where EndCodigo = ?";
			dao.executeCommand(sql, o.getLogradouro().getCodigo(), o.getNumero(), o.getComplemento(), o.getCodigo());
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
	}

	@Override
	public void del(int codigo) throws SQLException {
		try {
			dao.begin();
			sql = "Delete From TbEndereco Where EndCodigo = ?";
			dao.executeCommand(sql, codigo);
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
	}

	@Override
	public Endereco set(ResultSet rs) throws SQLException {
		DaoLogradouro logradouro = new DaoLogradouro();
		Endereco endereco = new Endereco();
		endereco.setCodigo(rs.getInt("EndCodigo"));
		endereco.setLogradouro(logradouro.get(rs.getInt("LogCodigo")));
		endereco.setNumero(rs.getString("EndNumero"));
		endereco.setComplemento(rs.getString("EndComplemento"));
		return endereco;
	}

	@Override
	public Endereco get(int codigo) throws SQLException {
		Endereco endereco = null;
		sql = "Select EndCodigo, LogCodigo, EndNumero, EndComplemento From TbEndereco Where EndCodigo = ?";
		ResultSet rs = dao.executeQuery(sql, codigo);
		if (rs.next()) {
			endereco = set(rs);
		}
		rs.close();
		return endereco;
	}

	@Override
	public Endereco get(Object param) throws SQLException {
		Endereco endereco = null;
		sql = "Select EndCodigo, LogCodigo, EndNumero, EndComplemento From TbEndereco Where EndNumero = ?";
		ResultSet rs = dao.executeQuery(sql, param);
		if (rs.next()) {
			endereco = set(rs);
		}
		rs.close();
		return endereco;
	}

	@Override
	public List<Endereco> list() throws SQLException {
		List<Endereco> enderecos = new LinkedList<Endereco>();
		sql = "Select EndCodigo, LogCodigo, EndNumero, EndComplemento From TbEndereco";
		ResultSet rs = dao.executeQuery(sql);
		while (rs.next()) {
			enderecos.add(set(rs));
		}
		rs.close();
		return enderecos;
	}

	@Override
	public List<Endereco> list(Object... params) throws SQLException {
		List<Endereco> enderecos = new LinkedList<Endereco>();
		sql = "Select EndCodigo, LogCodigo, EndNumero, EndComplemento From TbEndereco Where EndNumero Like ?";
		ResultSet rs = dao.executeQuery(sql, "%" + params[0] + "%");
		while (rs.next()) {
			enderecos.add(set(rs));
		}
		rs.close();
		return enderecos;
	}

}
